package sein.android.hopenote;

import java.io.ObjectOutputStream.PutField;
import java.util.ArrayList;

import sein.android.hopenote.Const.HopeNoteDb;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.UrlQuerySanitizer.ValueSanitizer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;


public class Note_Search extends Activity {
	
	private DBAdapter HnopenHelper;
	private SQLiteDatabase sdb;
	private String str_Item, str_User, str_Unit, str_Cnt;
	private	Cursor list_cur;
	private	TextView tv_item, tv_unit;
	private	ListView lv_search;
	private	ArrayList<SearchData> Search_arr;
	private SearchAdapter S_adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.note_search);
		
		HnopenHelper = new DBAdapter(this);
        sdb = HnopenHelper.getReadableDatabase();

        Intent MainActivity = getIntent();
		//str_User = getString(R.string.user_name);	// 사용자 명
        str_User = HopeNoteDb.CURRENT_USER;	// 사용자 명
		str_Item = MainActivity.getStringExtra(HopeNoteDb.EXTRA_SEARCH_ITEM);	// 항목 명
		str_Unit = MainActivity.getStringExtra(HopeNoteDb.EXTRA_SEARCH_UNIT);	// 단위
		str_Cnt = MainActivity.getStringExtra(HopeNoteDb.EXTRA_SEARCH_CNT);	// 입력 수
		
		// DB
		String getColumns[] = {HopeNoteDb.COL_DATE,HopeNoteDb.COL_DOCUMENT1,HopeNoteDb.COL_DOCUMENT2};
        String qWhere = HopeNoteDb.COL_USER + " = '" + str_User + "' AND " + HopeNoteDb.COL_ITEM + " = '" + str_Item + "'";
        list_cur = sdb.query(HopeNoteDb.TB_NOTE, getColumns, qWhere, null, null, null, null);
        list_cur.moveToFirst();
        
        // Insert into Array
        Search_arr = new ArrayList<SearchData>();
        SearchData datarow;
        for(int i = 0; i < list_cur.getCount(); i++){
        	datarow = new SearchData(list_cur.getString(0),list_cur.getString(1),list_cur.getString(2));
        	Search_arr.add(datarow);
        	list_cur.moveToNext();
        }
        list_cur.close();
        
        // Custom Adapter
        S_adapter = new SearchAdapter(this, R.layout.note_search_row,Search_arr);
        
        tv_item = (TextView)findViewById(R.id.tv_item);
        tv_item.setText(str_Item);
        tv_unit = (TextView)findViewById(R.id.tv_item);
        tv_unit.setText(str_Unit);
        lv_search = (ListView)findViewById(R.id.lv_search);
        lv_search.setAdapter(S_adapter);
	}
	
	
	
	//검색 결과 (날짜/값) 데이터 클래스
	class SearchData{
		String date;
		String document1;
		String document2;
		SearchData(String _date, String _document1, String _document2){
			date = _date;
			document1 = _document1;
			document2 = _document2;
		}
	}
	
	//검색 결과 어댑터 클래스
	class SearchAdapter extends BaseAdapter{
		Context context;
		int layoutId;
		ArrayList<SearchData> ResultDataArr;
		LayoutInflater Inflater;
		SearchAdapter(Context _context, int _layoutId, ArrayList<SearchData> _ResultDataArr){
			context = _context;
			layoutId = _layoutId;
			ResultDataArr = _ResultDataArr;
			Inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
		
		public int getCount(){
			return ResultDataArr.size();
		}
		
		public String getItem(int position){
			return ResultDataArr.get(position).date;
		}
		
		public long getItemId(int position)
		{
			return position;
		}
		
		public View getView(int position, View convertView, ViewGroup parent){
			final int pos = position;
			if(convertView == null){
				convertView = Inflater.inflate(layoutId, parent,false);
			}
			
			TextView tv_date = (TextView)convertView.findViewById(R.id.tv_date);
			tv_date.setText(ResultDataArr.get(position).date);
			TextView tv_document1 = (TextView)convertView.findViewById(R.id.tv_document1);
			tv_document1.setText(ResultDataArr.get(position).document1);
			TextView tv_document2 = (TextView)convertView.findViewById(R.id.tv_document2);
			tv_document2.setText(ResultDataArr.get(position).document2);
			if(str_Cnt == "1")
			{
				tv_document2.setVisibility(View.INVISIBLE);
			}
			
			tv_date.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(Note_Search.this, Note_Add.class);
					intent.putExtra(HopeNoteDb.EXTRA_NOTE_ADD_DATE, ResultDataArr.get(pos).date);
					startActivityForResult(intent, HopeNoteDb.REQ_NOTE_ADD);
				}
			});
			tv_document1.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(Note_Search.this, Note_Add.class);
					intent.putExtra(HopeNoteDb.EXTRA_NOTE_ADD_DATE, ResultDataArr.get(pos).date);
					startActivityForResult(intent, HopeNoteDb.REQ_NOTE_ADD);
				}
			});
			tv_document2.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(Note_Search.this, Note_Add.class);
					intent.putExtra(HopeNoteDb.EXTRA_NOTE_ADD_DATE, ResultDataArr.get(pos).date);
					startActivityForResult(intent, HopeNoteDb.REQ_NOTE_ADD);
				}
			});
			return convertView;
		}
	}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode){
		case HopeNoteDb.REQ_NOTE_ADD:
			if(resultCode == RESULT_OK){
				str_Item = data.getStringExtra(HopeNoteDb.EXTRA_SEARCH_ITEM);
				str_Unit = data.getStringExtra(HopeNoteDb.EXTRA_SEARCH_UNIT);
				str_Cnt = data.getStringExtra(HopeNoteDb.EXTRA_SEARCH_CNT);	// 입력 수
				// DB
				String getColumns[] = {HopeNoteDb.COL_DATE,HopeNoteDb.COL_DOCUMENT1,HopeNoteDb.COL_DOCUMENT2};
		        String qWhere = HopeNoteDb.COL_USER + " = '" + str_User + "' AND " + HopeNoteDb.COL_ITEM + " = '" + str_Item + "'";
		        list_cur = sdb.query(HopeNoteDb.TB_NOTE, getColumns, qWhere, null, null, null, null);
		        list_cur.moveToFirst();
		        
		        Search_arr.clear();
		        SearchData datarow;
		        for(int i = 0; i < list_cur.getCount(); i++){
		        	datarow = new SearchData(list_cur.getString(0),list_cur.getString(1),list_cur.getString(2));
		        	Search_arr.add(datarow);
		        	list_cur.moveToNext();
		        }
		        list_cur.close();
		        
		        S_adapter.notifyDataSetChanged();
			}
			break;
		}
	}
}
